/*
 * @Description: 分享插件
 */
;
(function($, window, document, undefined) {
    //插件初始化
    function init(target, options) {
        var settings = $.extend({}, $.fn.socialShare.defaults, options);
        //初始化各个组件
        console.log(settings);
        var $msb_main = "<p class='msb_main'>分享至</p>";
        var $social_group = "<div class='social_group'>" +
            "<a class='msb_network_button weixin'></a>" +
            "<a class='msb_network_button sina'></a>" +
            "<a class='msb_network_button qZone'></a>" +
            "<div id='weixin'></div>" +
            "</div>";
        $(target).append($msb_main);
        $(target).append($social_group);
        $(target).addClass("socialShare");
        $("#weixin").hide();
        //添加QQ空间分享事件
        $(document).on("click", ".msb_network_button.qZone", function() {
            qZone(this, settings);
        });
        //添加新浪微博分享事件
        $(document).on("click", ".msb_network_button.sina", function() {
            sinaWeibo(this, settings);
        });
        //添加微信分享事件
        $(".msb_network_button.weixin").hover(function() {
            weixinShare(this, settings);
        },function(){
            $("#weixin").hide();
        });

    }
    function replaceAPI(api, options) {
        api = api.replace('{url}', options.url);
        api = api.replace('{title}', options.title);
        api = api.replace('{content}', options.content);
        api = api.replace('{pic}', options.pic);
        return api;
    }

    function OPenWindow(URL) {
        var openUrl = URL; //弹出窗口的url
        var iWidth = 630; //弹出窗口的宽度;
        var iHeight = 580; //弹出窗口的高度;
        var iTop = (window.screen.availHeight - 30 - iHeight) / 2; //获得窗口的垂直位置;
        var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; //获得窗口的水平位置;
        window.open(openUrl, "", "height=" + iHeight + ", width=" + iWidth + ", top=" + iTop + ", left=" + iLeft + "" + ",toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
        //window.open('page.html', 'newwindow', 'height=580, width=650, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
    }

    function qZone(target, options) {
        var options = $.extend({}, $.fn.socialShare.defaults, options);
        OPenWindow(replaceAPI(qzone, options));
        //  window.open(replaceAPI(qzone, options));
    }

    function sinaWeibo(target, options) {
        var options = $.extend({}, $.fn.socialShare.defaults, options);
        OPenWindow(replaceAPI(sina, options));
        //  window.open(replaceAPI(sina, options));
    }

    function weixinShare(target, options) {
        var options = $.extend({}, $.fn.socialShare.defaults, options);
        console.log(options);
        showWX(replaceAPI(weixin, options));

        //  window.open(replaceAPI(weixin, ));
    }

    function showWX(url) {
        var weixing = 
            '<div class="erweima">' +
            '<img class="erweimas" src="" />' +
            '</div>' +
            '<p class="msgs">用微信扫描二维码，<br>分享至微信好友和朋友圈</p>';
        $("#weixin").html(weixing);
        $(".erweimas").attr('src', url);
        $("#weixin").show();
    }
    $.fn.socialShare = function(options, param) {
        if(typeof options == 'string') {
            var method = $.fn.socialShare.methods[options];
            if(method)
                return method(this, param);
        } else
            init(this, options);
    }

    //插件默认参数
    $.fn.socialShare.defaults = {
        url: window.location.href,
        title: document.title,
        content: '',
        pic: ''
    }

    //插件方法
    $.fn.socialShare.methods = {
        //初始化方法
        init: function(jq, options) {
            return jq.each(function() {
                init(this, options);
            });
        },
        qZone: function(jq, options) {
            return jq.each(function() {
                qZone(this, options);
            })
        },
        sinaWeibo: function(jq, options) {
            return jq.each(function() {
                sinaWeibo(this, options);
            });
        },
        weixinShare: function(jq, options) {
            return jq.each(function() {
                weixinShare(this, options);
            });
        }
    }

    //分享地址
    var qzone = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={url}&title={title}&pics={pic}&summary={content}';
    var sina = 'http://service.weibo.com/share/share.php?url={url}&title={title}&pic={pic}&searchPic=false';
    var weixin = 'http://qr.liantu.com/api.php?text={url}'; 
})(jQuery, window, document);